package com.common.util.mypage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: zyj
 * @create: 2020-01-07 11:05
 **/
public class Pages extends SimplePage {
    private static final long serialVersionUID = -6190845403265328029L;

    private boolean isFirstPage = true;//是否是第一页
    private boolean isLastPage = false;//是否是最后一页
    private int pageCount = 0;//当前页总记录数
    private int totalPage = 0;//总页数
    private int prePage = 1;//上一页页码
    private int nextPage = 1;//下一页页码

   private Map<String, Object> params = new HashMap<String, Object>();

    public Pages() {
        super();
    }

    public Pages(int pageNumber, int pageSize) {
        super();
        setPageNo(pageNumber);
        setPageSize(pageSize);
    }

    /**获取所有数据后分页，假分页*/
    public Pages(int pageNumber, int pageSize,List data) {
        super();
        setPageNo(pageNumber);
        setPageSize(pageSize);
        setTotal(data.size());
        if(data!=null && data.size()>0){
            //总页数
            totalPage = (int)Math.ceil(total/(double) pageSize);
            //第一页
            if(this.pageNo==1){
                isFirstPage=true;
            }else{
                isFirstPage=false;
            }
            //是否最后一页
            if(pageNo==totalPage){
                isLastPage = true;
            }else{
                isLastPage = false;
            }
            //下一页
            if(isLastPage){
                nextPage = 1;
            }else{
                nextPage = pageNo+1;
            }

            //是否最后一页
            if(pageNumber==totalPage){
                isLastPage = true;
            }

            //当前页数据
            int start = pageSize*(pageNumber-1);
            int end = pageSize*pageNumber;
            if(end>data.size()){
                end = data.size();
            }
            setRows(data.subList(start, end));
        }
    }


    @Override
    public Pages setRows(List data){
        super.setRows(data);
        if(data!=null && data.size()>0){
            pageCount = data.size();
            if(this.pageNo==1){
                isFirstPage=true;
            }else{
                isFirstPage=false;
            }
            //***
            totalPage = (int)Math.ceil(total/(double) pageSize);
            //***
            if(pageNo==totalPage){
                isLastPage = true;
            }else{
                isLastPage = false;
            }
            //***
            if(isFirstPage){
                prePage = 1;
            }else{
                prePage = pageNo-1;
            }
            //***
            if(isLastPage){
                nextPage = 1;
            }else{
                nextPage = pageNo+1;
            }
        }else{
            isLastPage = true;
        }
        return this;
    }


    /**只要get方法**/
    public boolean isFirstPage() {
        return isFirstPage;
    }

    public boolean isLastPage() {
        return isLastPage;
    }

    public int getPageCount() {
        return pageCount;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getPrePage() {
        return prePage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }
}
